/**
 * 
 */
package com.liusoft.android.utils;

import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 配置信息工具类
 * @author Winter Lau
 */
public class PreferencesUtils {

	private final static String DEFAULT_NAME = "_this";
	
	private final Context context;
	private final String name;

	public PreferencesUtils(Context ctx) {
		this(ctx, null);
	}
	
	public PreferencesUtils(Context ctx, String name) {
		this.context = ctx;
		this.name = (name==null)?DEFAULT_NAME:name;
	}
	
	public Map<String,?> all() {
		SharedPreferences settings = context.getSharedPreferences(name, 0);
		return settings.getAll();
	}
	
	public void remove(String key) {
		SharedPreferences settings = context.getSharedPreferences(name, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.remove(key);
    	editor.commit();
	}

	public void clear(String key) {
		SharedPreferences settings = context.getSharedPreferences(name, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.clear();
    	editor.commit();
	}
	
	public String read(String key, String defaultValue){
		SharedPreferences settings = context.getSharedPreferences(name, 0);
		return settings.getString(key, defaultValue);
	}
	
	public void write(String key, String value) {
		SharedPreferences settings = context.getSharedPreferences(name, 0);
		SharedPreferences.Editor editor = settings.edit();
    	editor.putString(key, value);
    	editor.commit();
	}

	public int read(String key, int defaultValue){
		SharedPreferences settings = context.getSharedPreferences(name, 0);
		return settings.getInt(key, defaultValue);
	}
	
	public void write(String key, int value) {
		SharedPreferences settings = context.getSharedPreferences(name, 0);
		SharedPreferences.Editor editor = settings.edit();
    	editor.putInt(key, value);
    	editor.commit();
	}

	public boolean read(String key, boolean defaultValue){
		SharedPreferences settings = context.getSharedPreferences(name, 0);
		return settings.getBoolean(key, defaultValue);
	}
	
	public void write(String key, boolean value) {
		SharedPreferences settings = context.getSharedPreferences(name, 0);
		SharedPreferences.Editor editor = settings.edit();
    	editor.putBoolean(key, value);
    	editor.commit();
	}

	public float read(String key, float defaultValue){
		SharedPreferences settings = context.getSharedPreferences(name, 0);
		return settings.getFloat(key, defaultValue);
	}
	
	public void write(String key, float value) {
		SharedPreferences settings = context.getSharedPreferences(name, 0);
		SharedPreferences.Editor editor = settings.edit();
    	editor.putFloat(key, value);
    	editor.commit();
	}

	public long read(String key, long defaultValue){
		SharedPreferences settings = context.getSharedPreferences(name, 0);
		return settings.getLong(key, defaultValue);
	}
	
	public void write(String key, long value) {
		SharedPreferences settings = context.getSharedPreferences(name, 0);
		SharedPreferences.Editor editor = settings.edit();
    	editor.putLong(key, value);
    	editor.commit();
	}
	
}
